package cn.kow.kmall.service.impl;

import cn.kow.kmall.core.constant.KmallStatusConstant;
import cn.kow.kmall.dao.UserRepository;
import cn.kow.kmall.pojo.dto.UserDTO;
import cn.kow.kmall.pojo.dto.UserInfoDTO;
import cn.kow.kmall.pojo.dto.UserQueryDTO;
import cn.kow.kmall.pojo.entity.User;
import cn.kow.kmall.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User getUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public User getUserByUid(Long uid) {
        return userRepository.findByUid(uid).orElse(null);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userRepository.findByPhone(phone).orElse(null);
    }

    @Override
    public User getUserByOpenid(String openid) {
        User exampleUser = new User();
        exampleUser.setStatus(KmallStatusConstant.NORMAL);
        exampleUser.setOpenid(openid);
        Optional<User> optional = userRepository.findOne(Example.of(exampleUser));
        return optional.orElse(null);
    }

    @Override
    public User saveWexinUser(String openid, String unionid, UserInfoDTO userInfo) {
        User user = new User();
        user.setUid(generateUid());

        user.setOpenid(openid);
        user.setUnionid(unionid);
        user.setNickname(userInfo.getNickname());
        user.setAvatar(userInfo.getAvatar());
        user.setName(userInfo.getNickname());
        user.setBirthday("");
        user.setPhone("");
        user.setEmail("");
        user.setGender(userInfo.getGender());
        user.setCity(userInfo.getCity());
        user.setProvince(userInfo.getProvince());
        user.setCountry(userInfo.getCountry());
        user.setStatus(KmallStatusConstant.NORMAL);
        user.setCreateTime(new Date());
        user = userRepository.save(user);
        return user;
    }

    @Override
    public User saveUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setUid(generateUid());
        user.setName(userDTO.getNickname());
        user.setBirthday("");
        user.setEmail("");
        user.setStatus(KmallStatusConstant.NORMAL);
        user.setCreateTime(new Date());
        return userRepository.save(user);
    }

    @Override
    public Page<User> listByPage(UserQueryDTO queryDTO) {
        return userRepository.queryByPage(queryDTO);
    }

    private Long generateUid() {
        Long uid = userRepository.findMaxUid();
        if (null == uid) {
            return 10000000L;
        }
        return uid + 1;
    }

}
